nhanes_raw <- read.csv("nhanes_imputed_1.csv")
# Define column renaming
nhanes <- nhanes_raw %>%
rename(
respondent_id = SEQN,
depression_q1 = DPQ010,
depression_q2 = DPQ020,
depression_q3 = DPQ030,
depression_q4 = DPQ040,
depression_q5 = DPQ050,
depression_q6 = DPQ060,
depression_q7 = DPQ070,
depression_q8 = DPQ080,
depression_q9 = DPQ090,
depression_q10 = DPQ100,
survey_year = SDDSRVYR,
respondent_status = RIDSTATR,
gender = RIAGENDR,
age_years = RIDAGEYR,
ethnicity_group1 = RIDRETH1,
ethnicity_group3 = RIDRETH3,
exam_month = RIDEXMON,
exam_age_months = RIDEXAGM,
military_status = DMQMILIZ,
birth_place = DMDBORN4,
years_in_usa = DMDYRUSR,
education_level = DMDEDUC2,
marital_status = DMDMARTZ,
pregnancy_status = RIDEXPRG,
household_size = DMDHHSIZ,
household_gender = DMDHRGND,
household_relationship_age = DMDHRAGZ,
household_relationship_education = DMDHREDZ,
household_relationship_marital = DMDHRMAZ,
household_relationship_employment = DMDHSEDZ,
interview_weight_2yr = WTINT2YR,
exam_weight_2yr = WTMEC2YR,
sample_stratum = SDMVSTRA,
sample_psu = SDMVPSU,
income_to_poverty_ratio = INDFMPIR,
phq9_total_score = PHQ9_TOTAL
)
colnames(nhanes)
## [1] "respondent_id" "depression_q1"
## [3] "depression_q2" "depression_q3"
## [5] "depression_q4" "depression_q5"
## [7] "depression_q6" "depression_q7"
## [9] "depression_q8" "depression_q9"
## [11] "depression_q10" "survey_year"
## [13] "respondent_status" "gender"
## [15] "age_years" "ethnicity_group1"
## [17] "ethnicity_group3" "exam_month"
## [19] "exam_age_months" "military_status"
## [21] "birth_place" "years_in_usa"
## [23] "education_level" "marital_status"
## [25] "pregnancy_status" "household_size"
## [27] "household_gender" "household_relationship_age"
## [29] "household_relationship_education" "household_relationship_marital"
## [31] "household_relationship_employment" "interview_weight_2yr"
## [33] "exam_weight_2yr" "sample_stratum"
## [35] "sample_psu" "income_to_poverty_ratio"
## [37] "phq9_total_score"
interest_cols <- c("income_to_poverty_ratio", "education_level", "gender",
"marital_status", "age_years")
## selecting an immigrant sibsection
immigrant_df <- nhanes %>%
filter(birth_place == 2 & !(years_in_usa %in% c(77, 99))) %>%
select(years_in_usa, phq9_total_score, gender, income_to_poverty_ratio,
marital_status, education_level, marital_status, age_years, sample_psu)
# creating a binary phq-9 outcome
immigrant_df <- immigrant_df %>%
mutate(phq9_binary = ifelse(phq9_total_score >= (median(phq9_total_score)), 1, 0))
immigrant_df$phq9_binary <- factor(immigrant_df$phq9_binary,
levels = c(0, 1),
labels = c("Low Depression", "High Depression"))
# releveling years in the US
immigrant_df$us_years_factor <- factor(immigrant_df$years_in_usa,
levels = c(1, 2, 3, 4, 5, 6),
labels = c("Less than 1 year",
"1 to 4 years",
"5 to 9 years",
"10 to 14 years",
"15 to 19 years",
"20 years or more"))
immigrant_df$us_years_factor <- relevel(immigrant_df$us_years_factor, ref = "Less than 1 year")
immigrant_df$us_years_factor
## [1] 20 years or more 20 years or more 20 years or more 20 years or more
## [5] 20 years or more 20 years or more 10 to 14 years 10 to 14 years
## [9] 20 years or more 20 years or more 1 to 4 years 20 years or more
## [13] 20 years or more 20 years or more 1 to 4 years 20 years or more
## [17] 5 to 9 years 20 years or more 10 to 14 years 10 to 14 years
## [21] Less than 1 year 1 to 4 years 1 to 4 years 20 years or more
## [25] 10 to 14 years Less than 1 year 20 years or more 20 years or more
## [29] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [33] 20 years or more 20 years or more 5 to 9 years 5 to 9 years
## [37] 20 years or more 1 to 4 years 1 to 4 years 20 years or more
## [41] 5 to 9 years 1 to 4 years 20 years or more 20 years or more
## [45] 15 to 19 years 5 to 9 years 20 years or more 20 years or more
## [49] 20 years or more 20 years or more 10 to 14 years 20 years or more
## [53] 10 to 14 years 20 years or more 15 to 19 years 20 years or more
## [57] 20 years or more 1 to 4 years 1 to 4 years 15 to 19 years
## [61] 20 years or more 20 years or more 20 years or more 20 years or more
## [65] 10 to 14 years 20 years or more 5 to 9 years 20 years or more
## [69] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [73] 20 years or more 20 years or more 20 years or more 20 years or more
## [77] 20 years or more 20 years or more 20 years or more 20 years or more
## [81] 20 years or more 20 years or more 20 years or more 20 years or more
## [85] 20 years or more 10 to 14 years 20 years or more 20 years or more
## [89] 10 to 14 years Less than 1 year 15 to 19 years 20 years or more
## [93] 15 to 19 years 1 to 4 years 20 years or more 20 years or more
## [97] 20 years or more 20 years or more 15 to 19 years 5 to 9 years
## [101] 20 years or more 20 years or more 15 to 19 years 1 to 4 years
## [105] 20 years or more Less than 1 year 20 years or more 20 years or more
## [109] 10 to 14 years 10 to 14 years 5 to 9 years 20 years or more
## [113] 20 years or more 20 years or more 1 to 4 years 20 years or more
## [117] 20 years or more 20 years or more 20 years or more 1 to 4 years
## [121] 20 years or more 20 years or more Less than 1 year Less than 1 year
## [125] 15 to 19 years 20 years or more 15 to 19 years 20 years or more
## [129] 1 to 4 years 1 to 4 years 20 years or more 5 to 9 years
## [133] 10 to 14 years 20 years or more 5 to 9 years 20 years or more
## [137] 15 to 19 years 1 to 4 years 20 years or more 20 years or more
## [141] 15 to 19 years 20 years or more 20 years or more 1 to 4 years
## [145] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [149] 20 years or more 20 years or more 20 years or more 1 to 4 years
## [153] 20 years or more 20 years or more 10 to 14 years 20 years or more
## [157] 15 to 19 years 15 to 19 years 20 years or more 5 to 9 years
## [161] 15 to 19 years 1 to 4 years 20 years or more 10 to 14 years
## [165] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [169] 20 years or more 10 to 14 years Less than 1 year 20 years or more
## [173] 20 years or more 20 years or more 1 to 4 years 5 to 9 years
## [177] 20 years or more 20 years or more 20 years or more 1 to 4 years
## [181] 15 to 19 years 20 years or more 1 to 4 years 20 years or more
## [185] 20 years or more 20 years or more 20 years or more 20 years or more
## [189] 20 years or more 5 to 9 years 20 years or more Less than 1 year
## [193] 20 years or more 15 to 19 years 5 to 9 years 15 to 19 years
## [197] Less than 1 year 1 to 4 years 20 years or more 20 years or more
## [201] 20 years or more 20 years or more 20 years or more 20 years or more
## [205] 5 to 9 years 1 to 4 years 20 years or more 20 years or more
## [209] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [213] 20 years or more 20 years or more 20 years or more 20 years or more
## [217] 20 years or more 15 to 19 years 15 to 19 years 15 to 19 years
## [221] 10 to 14 years 20 years or more 5 to 9 years 1 to 4 years
## [225] Less than 1 year 15 to 19 years 20 years or more 15 to 19 years
## [229] 5 to 9 years 15 to 19 years 5 to 9 years 1 to 4 years
## [233] 20 years or more 20 years or more 20 years or more 20 years or more
## [237] 20 years or more 20 years or more 15 to 19 years 20 years or more
## [241] Less than 1 year 20 years or more 20 years or more 20 years or more
## [245] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [249] 20 years or more 20 years or more 20 years or more 1 to 4 years
## [253] 15 to 19 years 10 to 14 years 20 years or more 20 years or more
## [257] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [261] 20 years or more 20 years or more 20 years or more 20 years or more
## [265] 20 years or more 1 to 4 years 10 to 14 years 20 years or more
## [269] Less than 1 year 5 to 9 years 20 years or more 20 years or more
## [273] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [277] 15 to 19 years 20 years or more 10 to 14 years 20 years or more
## [281] 20 years or more 20 years or more 20 years or more 15 to 19 years
## [285] 20 years or more 20 years or more 15 to 19 years 20 years or more
## [289] 20 years or more 5 to 9 years 20 years or more 15 to 19 years
## [293] 20 years or more 20 years or more Less than 1 year 15 to 19 years
## [297] 20 years or more 20 years or more 1 to 4 years 15 to 19 years
## [301] 20 years or more 20 years or more 20 years or more 20 years or more
## [305] 20 years or more 15 to 19 years 20 years or more 1 to 4 years
## [309] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [313] 20 years or more 5 to 9 years 15 to 19 years 20 years or more
## [317] 20 years or more 5 to 9 years 20 years or more 10 to 14 years
## [321] 20 years or more 20 years or more 20 years or more 20 years or more
## [325] 5 to 9 years 15 to 19 years 20 years or more 5 to 9 years
## [329] 20 years or more 5 to 9 years 1 to 4 years 20 years or more
## [333] 20 years or more 15 to 19 years 5 to 9 years 20 years or more
## [337] 20 years or more Less than 1 year 20 years or more 10 to 14 years
## [341] 20 years or more 20 years or more 10 to 14 years Less than 1 year
## [345] 1 to 4 years 10 to 14 years 20 years or more 10 to 14 years
## [349] 20 years or more 5 to 9 years 20 years or more 15 to 19 years
## [353] 5 to 9 years 20 years or more 1 to 4 years 1 to 4 years
## [357] 20 years or more Less than 1 year 20 years or more 5 to 9 years
## [361] 1 to 4 years 20 years or more 15 to 19 years 5 to 9 years
## [365] 20 years or more 10 to 14 years 20 years or more 20 years or more
## [369] 20 years or more 20 years or more 20 years or more 1 to 4 years
## [373] 15 to 19 years 20 years or more 20 years or more 15 to 19 years
## [377] 10 to 14 years 20 years or more 5 to 9 years 20 years or more
## [381] 20 years or more 20 years or more 1 to 4 years 20 years or more
## [385] 15 to 19 years 20 years or more 20 years or more 20 years or more
## [389] 5 to 9 years 10 to 14 years 1 to 4 years 20 years or more
## [393] 15 to 19 years 20 years or more Less than 1 year 20 years or more
## [397] 20 years or more 20 years or more 1 to 4 years 1 to 4 years
## [401] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [405] 20 years or more 20 years or more 15 to 19 years 20 years or more
## [409] 20 years or more 20 years or more 20 years or more 20 years or more
## [413] 20 years or more 20 years or more 20 years or more 20 years or more
## [417] 20 years or more 10 to 14 years 5 to 9 years 20 years or more
## [421] 5 to 9 years 20 years or more 20 years or more 1 to 4 years
## [425] 20 years or more 1 to 4 years 20 years or more 15 to 19 years
## [429] 20 years or more 1 to 4 years 1 to 4 years 20 years or more
## [433] 20 years or more 20 years or more 20 years or more 20 years or more
## [437] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [441] 15 to 19 years 20 years or more 5 to 9 years 1 to 4 years
## [445] Less than 1 year 20 years or more 10 to 14 years 10 to 14 years
## [449] 1 to 4 years 1 to 4 years 1 to 4 years 1 to 4 years
## [453] 20 years or more 20 years or more 20 years or more 15 to 19 years
## [457] 15 to 19 years 15 to 19 years 5 to 9 years 20 years or more
## [461] 15 to 19 years 1 to 4 years 15 to 19 years 1 to 4 years
## [465] 20 years or more 15 to 19 years 20 years or more 20 years or more
## [469] 20 years or more 20 years or more 20 years or more 5 to 9 years
## [473] 20 years or more 1 to 4 years 15 to 19 years 5 to 9 years
## [477] 1 to 4 years 5 to 9 years 1 to 4 years 20 years or more
## [481] 20 years or more 20 years or more Less than 1 year 1 to 4 years
## [485] 20 years or more 5 to 9 years 1 to 4 years 20 years or more
## [489] 20 years or more 5 to 9 years 20 years or more 10 to 14 years
## [493] 20 years or more 20 years or more 20 years or more 1 to 4 years
## [497] 20 years or more 20 years or more 20 years or more 20 years or more
## [501] 20 years or more 20 years or more 20 years or more 20 years or more
## [505] 1 to 4 years 15 to 19 years 1 to 4 years 15 to 19 years
## [509] 20 years or more 5 to 9 years 20 years or more 20 years or more
## [513] 20 years or more 20 years or more 20 years or more 20 years or more
## [517] 15 to 19 years 1 to 4 years 20 years or more 5 to 9 years
## [521] 20 years or more 20 years or more 20 years or more 5 to 9 years
## [525] 20 years or more 20 years or more 20 years or more 20 years or more
## [529] 5 to 9 years 1 to 4 years 20 years or more 5 to 9 years
## [533] 20 years or more 5 to 9 years 20 years or more 15 to 19 years
## [537] 15 to 19 years 15 to 19 years 1 to 4 years 20 years or more
## [541] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [545] 15 to 19 years 10 to 14 years 10 to 14 years 20 years or more
## [549] 15 to 19 years 20 years or more Less than 1 year 20 years or more
## [553] Less than 1 year 10 to 14 years 20 years or more 20 years or more
## [557] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [561] 20 years or more 15 to 19 years 20 years or more 10 to 14 years
## [565] Less than 1 year 5 to 9 years 10 to 14 years 20 years or more
## [569] 20 years or more 20 years or more 10 to 14 years 15 to 19 years
## [573] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [577] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [581] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [585] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [589] 20 years or more 10 to 14 years 20 years or more 10 to 14 years
## [593] 5 to 9 years 20 years or more 20 years or more 20 years or more
## [597] 20 years or more 5 to 9 years 1 to 4 years 20 years or more
## [601] 20 years or more 20 years or more Less than 1 year 1 to 4 years
## [605] 20 years or more 20 years or more 20 years or more 20 years or more
## [609] 5 to 9 years 20 years or more 20 years or more 15 to 19 years
## [613] 20 years or more 20 years or more 5 to 9 years 15 to 19 years
## [617] 15 to 19 years 15 to 19 years 20 years or more 5 to 9 years
## [621] 20 years or more 20 years or more 20 years or more Less than 1 year
## [625] 10 to 14 years 20 years or more Less than 1 year 20 years or more
## [629] 5 to 9 years 1 to 4 years 20 years or more 20 years or more
## [633] 20 years or more 15 to 19 years 20 years or more 20 years or more
## [637] 20 years or more 20 years or more 20 years or more Less than 1 year
## [641] 20 years or more 10 to 14 years 20 years or more 20 years or more
## [645] 20 years or more 20 years or more 20 years or more 20 years or more
## [649] 15 to 19 years 20 years or more 20 years or more 20 years or more
## [653] 15 to 19 years 20 years or more 20 years or more 20 years or more
## [657] 20 years or more 5 to 9 years 1 to 4 years 20 years or more
## [661] 20 years or more 5 to 9 years 10 to 14 years Less than 1 year
## [665] 5 to 9 years 15 to 19 years 20 years or more 20 years or more
## [669] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [673] 5 to 9 years 20 years or more 20 years or more 10 to 14 years
## [677] 5 to 9 years 20 years or more Less than 1 year 5 to 9 years
## [681] 20 years or more 20 years or more 15 to 19 years 10 to 14 years
## [685] Less than 1 year 20 years or more 1 to 4 years 20 years or more
## [689] 10 to 14 years 20 years or more 1 to 4 years 20 years or more
## [693] 1 to 4 years 20 years or more 15 to 19 years 20 years or more
## [697] 5 to 9 years 20 years or more 5 to 9 years 1 to 4 years
## [701] 15 to 19 years 20 years or more 1 to 4 years 20 years or more
## [705] 20 years or more 5 to 9 years 15 to 19 years 20 years or more
## [709] 20 years or more 15 to 19 years 5 to 9 years 20 years or more
## [713] 20 years or more 20 years or more 20 years or more 15 to 19 years
## [717] 1 to 4 years 15 to 19 years 20 years or more 20 years or more
## [721] 20 years or more 20 years or more 10 to 14 years 15 to 19 years
## [725] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [729] 20 years or more 20 years or more Less than 1 year 20 years or more
## [733] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [737] 1 to 4 years 20 years or more 20 years or more 10 to 14 years
## [741] 20 years or more 5 to 9 years 5 to 9 years 15 to 19 years
## [745] 20 years or more 1 to 4 years 20 years or more 15 to 19 years
## [749] 20 years or more 1 to 4 years 15 to 19 years 20 years or more
## [753] 20 years or more 5 to 9 years 15 to 19 years 5 to 9 years
## [757] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [761] 5 to 9 years 20 years or more 20 years or more 20 years or more
## [765] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [769] 15 to 19 years 10 to 14 years 20 years or more 10 to 14 years
## [773] 20 years or more 20 years or more 10 to 14 years 1 to 4 years
## [777] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [781] 20 years or more 10 to 14 years 20 years or more 20 years or more
## [785] 20 years or more 15 to 19 years Less than 1 year 20 years or more
## [789] 15 to 19 years 20 years or more 10 to 14 years 20 years or more
## [793] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [797] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [801] 15 to 19 years 20 years or more 20 years or more 20 years or more
## [805] 20 years or more 20 years or more 20 years or more 20 years or more
## [809] 5 to 9 years 10 to 14 years 15 to 19 years 20 years or more
## [813] 20 years or more 20 years or more 20 years or more 20 years or more
## [817] 20 years or more 20 years or more 20 years or more 15 to 19 years
## [821] 5 to 9 years 20 years or more 20 years or more 20 years or more
## [825] 20 years or more 20 years or more 5 to 9 years 1 to 4 years
## [829] 20 years or more 20 years or more 5 to 9 years 5 to 9 years
## [833] 5 to 9 years 20 years or more 1 to 4 years 20 years or more
## [837] 10 to 14 years 5 to 9 years 20 years or more 15 to 19 years
## [841] 20 years or more 20 years or more 20 years or more 20 years or more
## [845] 20 years or more 20 years or more 20 years or more 20 years or more
## [849] 20 years or more 15 to 19 years 15 to 19 years 20 years or more
## [853] 20 years or more 20 years or more 20 years or more 20 years or more
## [857] 20 years or more 15 to 19 years 20 years or more 1 to 4 years
## [861] 20 years or more 20 years or more 20 years or more 20 years or more
## [865] 20 years or more 20 years or more 20 years or more 15 to 19 years
## [869] 1 to 4 years 20 years or more 20 years or more 5 to 9 years
## [873] Less than 1 year 5 to 9 years 20 years or more 5 to 9 years
## [877] 20 years or more 20 years or more 20 years or more 15 to 19 years
## [881] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [885] 20 years or more 5 to 9 years 20 years or more 10 to 14 years
## [889] 20 years or more 5 to 9 years 20 years or more 20 years or more
## [893] 20 years or more Less than 1 year Less than 1 year 20 years or more
## [897] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [901] 20 years or more Less than 1 year 20 years or more 20 years or more
## [905] 20 years or more 20 years or more 20 years or more 20 years or more
## [909] 15 to 19 years 15 to 19 years 20 years or more 10 to 14 years
## [913] 20 years or more 20 years or more 20 years or more 20 years or more
## [917] 1 to 4 years 20 years or more 20 years or more 20 years or more
## [921] 15 to 19 years 20 years or more 20 years or more 20 years or more
## [925] 20 years or more Less than 1 year 20 years or more 20 years or more
## [929] 10 to 14 years 20 years or more 15 to 19 years 20 years or more
## [933] 20 years or more Less than 1 year 20 years or more 1 to 4 years
## [937] 20 years or more 20 years or more 20 years or more 5 to 9 years
## [941] 5 to 9 years 20 years or more 10 to 14 years 20 years or more
## [945] 15 to 19 years 20 years or more 20 years or more 1 to 4 years
## [949] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [953] 20 years or more 20 years or more 20 years or more 20 years or more
## [957] 5 to 9 years 20 years or more 20 years or more 20 years or more
## [961] 20 years or more 1 to 4 years 1 to 4 years 20 years or more
## [965] 20 years or more 20 years or more 15 to 19 years 20 years or more
## [969] 20 years or more 15 to 19 years 5 to 9 years 5 to 9 years
## [973] 20 years or more 20 years or more 15 to 19 years 20 years or more
## [977] 5 to 9 years 15 to 19 years 20 years or more 20 years or more
## [981] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [985] 20 years or more 20 years or more 20 years or more 20 years or more
## [989] 20 years or more 5 to 9 years Less than 1 year 1 to 4 years
## [993] 1 to 4 years Less than 1 year 1 to 4 years 20 years or more
## [997] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [1001] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [1005] 20 years or more 20 years or more 20 years or more 20 years or more
## [1009] 20 years or more 20 years or more Less than 1 year 20 years or more
## [1013] 1 to 4 years 20 years or more 1 to 4 years 20 years or more
## [1017] 20 years or more 20 years or more Less than 1 year 15 to 19 years
## [1021] 15 to 19 years 20 years or more 20 years or more 20 years or more
## [1025] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [1029] 1 to 4 years 1 to 4 years 20 years or more 20 years or more
## [1033] 5 to 9 years 20 years or more 20 years or more 15 to 19 years
## [1037] 20 years or more 5 to 9 years 15 to 19 years 20 years or more
## [1041] 20 years or more 20 years or more 20 years or more 20 years or more
## [1045] 20 years or more 5 to 9 years 20 years or more 5 to 9 years
## [1049] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [1053] 20 years or more 5 to 9 years 20 years or more 20 years or more
## [1057] 15 to 19 years 1 to 4 years 1 to 4 years 15 to 19 years
## [1061] 15 to 19 years 15 to 19 years 20 years or more 20 years or more
## [1065] 20 years or more 15 to 19 years 20 years or more 20 years or more
## [1069] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [1073] 20 years or more 20 years or more 5 to 9 years 20 years or more
## [1077] 5 to 9 years 20 years or more 20 years or more 1 to 4 years
## [1081] 20 years or more 20 years or more Less than 1 year 20 years or more
## [1085] 20 years or more 1 to 4 years 20 years or more 20 years or more
## [1089] 20 years or more 20 years or more 20 years or more 20 years or more
## [1093] 5 to 9 years 20 years or more 20 years or more 10 to 14 years
## [1097] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [1101] 20 years or more 15 to 19 years 20 years or more 1 to 4 years
## [1105] 20 years or more 20 years or more 15 to 19 years 15 to 19 years
## [1109] 1 to 4 years 1 to 4 years 20 years or more 20 years or more
## [1113] 20 years or more 20 years or more 20 years or more 20 years or more
## [1117] 20 years or more 20 years or more 20 years or more 20 years or more
## [1121] 20 years or more 20 years or more 20 years or more 10 to 14 years
## [1125] 10 to 14 years 20 years or more Less than 1 year 5 to 9 years
## [1129] 20 years or more 20 years or more 20 years or more 20 years or more
## [1133] 20 years or more Less than 1 year 5 to 9 years 15 to 19 years
## [1137] 5 to 9 years 20 years or more Less than 1 year 20 years or more
## [1141] 10 to 14 years 5 to 9 years 20 years or more 20 years or more
## [1145] 20 years or more 1 to 4 years 20 years or more Less than 1 year
## [1149] 10 to 14 years 20 years or more 20 years or more 15 to 19 years
## [1153] 20 years or more 20 years or more 20 years or more 20 years or more
## [1157] 1 to 4 years 20 years or more 20 years or more 1 to 4 years
## [1161] 10 to 14 years 20 years or more 20 years or more 20 years or more
## [1165] 20 years or more 5 to 9 years 20 years or more 10 to 14 years
## [1169] 1 to 4 years 10 to 14 years 20 years or more 5 to 9 years
## [1173] 20 years or more 5 to 9 years 5 to 9 years 20 years or more
## [1177] 20 years or more 20 years or more 1 to 4 years Less than 1 year
## [1181] 20 years or more 15 to 19 years 20 years or more 20 years or more
## [1185] 1 to 4 years 20 years or more 20 years or more 10 to 14 years
## [1189] 20 years or more 1 to 4 years 20 years or more Less than 1 year
## [1193] 5 to 9 years 20 years or more 15 to 19 years 20 years or more
## [1197] 20 years or more 20 years or more 10 to 14 years 20 years or more
## [1201] 20 years or more Less than 1 year 5 to 9 years 5 to 9 years
## [1205] 15 to 19 years 1 to 4 years 20 years or more 1 to 4 years
## [1209] 20 years or more 5 to 9 years 15 to 19 years 20 years or more
## [1213] 1 to 4 years 20 years or more 15 to 19 years 20 years or more
## [1217] 10 to 14 years 20 years or more 15 to 19 years 20 years or more
## [1221] 20 years or more 20 years or more 20 years or more 5 to 9 years
## [1225] 10 to 14 years Less than 1 year 5 to 9 years 15 to 19 years
## [1229] Less than 1 year 15 to 19 years 20 years or more 20 years or more
## [1233] 1 to 4 years 20 years or more 1 to 4 years 5 to 9 years
## [1237] 20 years or more Less than 1 year 10 to 14 years 20 years or more
## [1241] 5 to 9 years 20 years or more 10 to 14 years 20 years or more
## 6 Levels: Less than 1 year 1 to 4 years 5 to 9 years ... 20 years or more
# logreg model
summary(glm(phq9_binary ~ us_years_factor + gender + income_to_poverty_ratio
+ marital_status + education_level + age_years,
family = binomial(link = "logit"), data = immigrant_df))
##
## Call:
## glm(formula = phq9_binary ~ us_years_factor + gender + income_to_poverty_ratio +
## marital_status + education_level + age_years, family = binomial(link = "logit"),
## data = immigrant_df)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 0.176726 0.440607 0.401 0.68835
## us_years_factor1 to 4 years 0.179836 0.338475 0.531 0.59520
## us_years_factor5 to 9 years 0.568862 0.344153 1.653 0.09834 .
## us_years_factor10 to 14 years 0.328377 0.361799 0.908 0.36408
## us_years_factor15 to 19 years 0.323951 0.340728 0.951 0.34172
## us_years_factor20 years or more 0.538390 0.307233 1.752 0.07971 .
## gender 0.356474 0.119124 2.992 0.00277 **
## income_to_poverty_ratio -0.061406 0.042742 -1.437 0.15081
## marital_status 0.278222 0.085507 3.254 0.00114 **
## education_level -0.124309 0.047943 -2.593 0.00952 **
## age_years -0.012982 0.004304 -3.016 0.00256 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 1692.3 on 1243 degrees of freedom
## Residual deviance: 1639.2 on 1233 degrees of freedom
## AIC: 1661.2
##
## Number of Fisher Scoring iterations: 4
# OLS Model - 20+ years significant
summary(lm(phq9_total_score ~ us_years_factor + gender + income_to_poverty_ratio
+ marital_status + education_level + age_years, data = immigrant_df))
##
## Call:
## lm(formula = phq9_total_score ~ us_years_factor + gender + income_to_poverty_ratio +
## marital_status + education_level + age_years, data = immigrant_df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.276 -2.945 -1.535 1.740 21.092
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 4.445641 0.965876 4.603 4.60e-06 ***
## us_years_factor1 to 4 years 0.263480 0.743494 0.354 0.723114
## us_years_factor5 to 9 years 0.474624 0.749232 0.633 0.526537
## us_years_factor10 to 14 years 0.078020 0.791018 0.099 0.921446
## us_years_factor15 to 19 years 0.110899 0.747397 0.148 0.882067
## us_years_factor20 years or more 0.519742 0.673837 0.771 0.440667
## gender 0.502779 0.260024 1.934 0.053393 .
## income_to_poverty_ratio -0.132260 0.093430 -1.416 0.157146
## marital_status 0.601767 0.181134 3.322 0.000919 ***
## education_level -0.486186 0.103751 -4.686 3.09e-06 ***
## age_years -0.015661 0.009256 -1.692 0.090904 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.498 on 1233 degrees of freedom
## Multiple R-squared: 0.04929, Adjusted R-squared: 0.04158
## F-statistic: 6.392 on 10 and 1233 DF, p-value: 1.205e-09
# anova - not sig
summary(anova_model <- aov(phq9_total_score ~ us_years_factor,
data = immigrant_df))
## Df Sum Sq Mean Sq F value Pr(>F)
## us_years_factor 5 5 1.038 0.049 0.999
## Residuals 1238 26230 21.187
## selecting an immigrant sibsection
race_df <- nhanes %>%
select(phq9_total_score, gender, income_to_poverty_ratio,
marital_status, education_level, marital_status, age_years,
ethnicity_group3) %>%
mutate(
ethnicity_group3 = recode(ethnicity_group3,
`1` = "Hispanic",
`2` = "Hispanic",
`3` = "White",
`4` = "Black",
`6` = "Asian",
`7` = "Other"))
# creating a binary phq-9 outcome
race_df <- race_df %>%
mutate(phq9_binary = ifelse(phq9_total_score >= (median(phq9_total_score)), 1, 0))
# logreg model
summary(glm(phq9_binary ~ ethnicity_group3 + gender + income_to_poverty_ratio
+ marital_status + education_level + age_years,
family = binomial(link = "logit"), data = race_df))
##
## Call:
## glm(formula = phq9_binary ~ ethnicity_group3 + gender + income_to_poverty_ratio +
## marital_status + education_level + age_years, family = binomial(link = "logit"),
## data = race_df)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.125149 0.191959 5.861 4.59e-09 ***
## ethnicity_group3Black -0.010562 0.135377 -0.078 0.9378
## ethnicity_group3Hispanic 0.023089 0.130163 0.177 0.8592
## ethnicity_group3Other 0.397446 0.155987 2.548 0.0108 *
## ethnicity_group3White 0.262348 0.116685 2.248 0.0246 *
## gender 0.515330 0.053633 9.608 < 2e-16 ***
## income_to_poverty_ratio -0.123999 0.019074 -6.501 7.99e-11 ***
## marital_status 0.005838 0.010361 0.563 0.5731
## education_level -0.061407 0.027718 -2.215 0.0267 *
## age_years -0.018669 0.001553 -12.023 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 8377.1 on 6336 degrees of freedom
## Residual deviance: 8036.2 on 6327 degrees of freedom
## AIC: 8056.2
##
## Number of Fisher Scoring iterations: 4
# linear model
summary(glm(phq9_total_score ~ ethnicity_group3 + gender + income_to_poverty_ratio
+ marital_status + education_level + age_years, data = race_df))
##
## Call:
## glm(formula = phq9_total_score ~ ethnicity_group3 + gender +
## income_to_poverty_ratio + marital_status + education_level +
## age_years, data = race_df)
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.596675 0.411127 16.045 < 2e-16 ***
## ethnicity_group3Black -0.029601 0.294168 -0.101 0.919851
## ethnicity_group3Hispanic 0.244132 0.282332 0.865 0.387238
## ethnicity_group3Other 1.090149 0.330975 3.294 0.000994 ***
## ethnicity_group3White 0.663557 0.254404 2.608 0.009121 **
## gender 0.982754 0.115515 8.508 < 2e-16 ***
## income_to_poverty_ratio -0.428409 0.040730 -10.518 < 2e-16 ***
## marital_status 0.057603 0.019860 2.900 0.003739 **
## education_level -0.290499 0.059223 -4.905 9.57e-07 ***
## age_years -0.042616 0.003238 -13.162 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for gaussian family taken to be 20.71894)
##
## Null deviance: 142364 on 6336 degrees of freedom
## Residual deviance: 131089 on 6327 degrees of freedom
## AIC: 37203
##
## Number of Fisher Scoring iterations: 2
# anova - sig!
summary(anova_model <- aov(phq9_total_score ~ ethnicity_group3,
data = race_df))
## Df Sum Sq Mean Sq F value Pr(>F)
## ethnicity_group3 4 757 189.17 8.459 8.38e-07 ***
## Residuals 6332 141607 22.36
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
viz_df <- nhanes %>%
select(interest_cols, phq9_total_score)
## Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
## ℹ Please use `all_of()` or `any_of()` instead.
## # Was:
## data %>% select(interest_cols)
##
## # Now:
## data %>% select(all_of(interest_cols))
##
## See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
cor_matrix <- cor(viz_df, use = "pairwise.complete.obs")
cor_data <- melt(cor_matrix)
cor_data
## Var1 Var2 value
## 1 income_to_poverty_ratio income_to_poverty_ratio 1.0000000000
## 2 education_level income_to_poverty_ratio 0.4872395606
## 3 gender income_to_poverty_ratio -0.0614431632
## 4 marital_status income_to_poverty_ratio -0.0735067666
## 5 age_years income_to_poverty_ratio 0.0865564244
## 6 phq9_total_score income_to_poverty_ratio -0.1961301645
## 7 income_to_poverty_ratio education_level 0.4872395606
## 8 education_level education_level 1.0000000000
## 9 gender education_level 0.0364302662
## 10 marital_status education_level -0.0215808319
## 11 age_years education_level -0.0696164511
## 12 phq9_total_score education_level -0.1173094710
## 13 income_to_poverty_ratio gender -0.0614431632
## 14 education_level gender 0.0364302662
## 15 gender gender 1.0000000000
## 16 marital_status gender 0.0001658986
## 17 age_years gender -0.0089622811
## 18 phq9_total_score gender 0.1103407052
## 19 income_to_poverty_ratio marital_status -0.0735067666
## 20 education_level marital_status -0.0215808319
## 21 gender marital_status 0.0001658986
## 22 marital_status marital_status 1.0000000000
## 23 age_years marital_status -0.0840859569
## 24 phq9_total_score marital_status 0.0591269943
## 25 income_to_poverty_ratio age_years 0.0865564244
## 26 education_level age_years -0.0696164511
## 27 gender age_years -0.0089622811
## 28 marital_status age_years -0.0840859569
## 29 age_years age_years 1.0000000000
## 30 phq9_total_score age_years -0.1683996150
## 31 income_to_poverty_ratio phq9_total_score -0.1961301645
## 32 education_level phq9_total_score -0.1173094710
## 33 gender phq9_total_score 0.1103407052
## 34 marital_status phq9_total_score 0.0591269943
## 35 age_years phq9_total_score -0.1683996150
## 36 phq9_total_score phq9_total_score 1.0000000000
colnames(cor_data) <- c("Variable 1", "Variable 2", "Correlation")
# Ensure column names are correctly referenced
p <-ggplot(cor_data, aes(x = `Variable 1`, y = `Variable 2`, fill = Correlation)) +
geom_tile(color = "white") +
scale_fill_gradient2(low = "#b2182b", mid = "white", high = "#2166ac", midpoint = 0) +
theme_minimal() +
theme(
axis.text.x = element_text(size = 6, angle = 30, hjust = 0, vjust = 0), # Adjust text angle
axis.text.y = element_text(size = 10), # Adjust y-axis labels
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.grid = element_blank(),
plot.title = element_text(hjust = 0.5)
) +
labs(title = "Depression Correlation Heatmap", fill = "Correlation")
ggplotly(p)
viz_long <- viz_df %>%
pivot_longer(cols = all_of(interest_cols), names_to = "name", values_to = "category")
ggplot(viz_long, aes(x = category, y = phq9_total_score)) +
geom_boxplot(fill = "#69b3a2", color = "black") +
facet_wrap(~ name, scales = "free_x") + # Separate facets for each variable
theme_minimal() +
theme(
strip.text = element_text(size = 12, face = "bold"), # Improve facet labels
axis.text.x = element_text(angle = 45, hjust = 1) # Rotate x-axis labels for readability
) +
labs(title = "PHQ-9 Total Score by Different Categorical Variables",
x = "Category",
y = "PHQ-9 Total Score")
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?
## Boxplot of martial status
marital_df <- viz_df%>%
filter(marital_status %in% c(1, 2, 3))
plot_ly(
data = marital_df,
x = ~marital_status,
y = ~phq9_total_score,
type = "box"
) %>%
layout(
title = "Interactive PHQ-9 Total Score by Unique Answers",
xaxis = list(title = "Marital Status"),
yaxis = list(title = "PHQ-9 Total Score"),
boxmode = "group"
)
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
viz_df <- viz_df %>%
mutate(income_category = cut(income_to_poverty_ratio,
breaks = seq(0, max(income_to_poverty_ratio,
na.rm = TRUE) + 1, by = 1),
labels = as.character(
1:(length(seq(0, max(income_to_poverty_ratio,
na.rm = TRUE), by = 1)))),
include.lowest = TRUE, right = FALSE))
summary(viz_df$income_category)
## 1 2 3 4 5 6
## 1080 1277 1053 810 580 1537
plot_ly(
data = viz_df,
x = ~income_category,
y = ~phq9_total_score,
type = "box"
) %>%
layout(
title = "Interactive PHQ-9 Total Score by Unique Answers",
xaxis = list(title = "income_to_poverty_ratio"),
yaxis = list(title = "PHQ-9 Total Score"),
boxmode = "group"
)
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
viz_df <- viz_df %>%
mutate(gender = recode(gender,
`1` = "Male",
`2` = "Female"
))
plot_ly(
data = viz_df,
x = ~gender,
y = ~phq9_total_score,
type = "box"
) %>%
layout(
title = "Interactive PHQ-9 Total Score by Unique Answers",
xaxis = list(title = "Gender"),
yaxis = list(title = "PHQ-9 Total Score"),
boxmode = "group"
)
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
plot_ly(
data = nhanes,
x = ~household_size,
y = ~phq9_total_score,
type = "box"
) %>%
layout(
title = "Interactive PHQ-9 Total Score by Unique Answers",
xaxis = list(title = "Household Size"),
yaxis = list(title = "PHQ-9 Total Score"),
boxmode = "group"
)
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
nhanes <- nhanes %>%
mutate(military_status = recode(military_status,
`1` = "Active",
`2` = "Not Active"
))
plot_ly(
data = nhanes,
x = ~military_status,
y = ~phq9_total_score,
type = "box"
) %>%
layout(
title = "Interactive PHQ-9 Total Score by Unique Answers",
xaxis = list(title = "Household Size"),
yaxis = list(title = "PHQ-9 Total Score"),
boxmode = "group"
)
## Warning: 'layout' objects don't have these attributes: 'boxmode'
## Valid attributes include:
## '_deprecated', 'activeshape', 'annotations', 'autosize', 'autotypenumbers', 'calendar', 'clickmode', 'coloraxis', 'colorscale', 'colorway', 'computed', 'datarevision', 'dragmode', 'editrevision', 'editType', 'font', 'geo', 'grid', 'height', 'hidesources', 'hoverdistance', 'hoverlabel', 'hovermode', 'images', 'legend', 'mapbox', 'margin', 'meta', 'metasrc', 'modebar', 'newshape', 'paper_bgcolor', 'plot_bgcolor', 'polar', 'scene', 'selectdirection', 'selectionrevision', 'separators', 'shapes', 'showlegend', 'sliders', 'smith', 'spikedistance', 'template', 'ternary', 'title', 'transition', 'uirevision', 'uniformtext', 'updatemenus', 'width', 'xaxis', 'yaxis', 'barmode', 'bargap', 'mapType'
polar_data <- viz_df %>%
count(education_level) %>% # Count occurrences
mutate(education_level = recode(education_level,
`1` = "Less than 9th grade",
`2` = "9-11th grade",
`3` = "HS/GED ",
`4` = "Some college",
`5` = "Bachelor's or More")) %>%
rename(category = education_level, value = n) %>%
mutate(angle = seq(0, 360, length.out = n())) # Calculate angles for each category
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `education_level = recode(...)`.
## Caused by warning:
## ! Unreplaced values treated as NA as `.x` is not compatible.
## Please specify replacements exhaustively or supply `.default`.
(polar_data$category)
## [1] "Less than 9th grade" "9-11th grade" "HS/GED "
## [4] "Some college" "Bachelor's or More" NA
# Create the polar bar chart
fig <- plot_ly(polar_data,
type = "barpolar",
r = ~value,
theta = ~angle,
text = ~paste(category, ":", value),
hoverinfo = "text",
marker = list(color = c("#1f78b4", "#33a02c", "#e31a1c", "#ff7f00", "#6a3d9a"))
)
fig <- fig %>%
layout(
title = list(
text = "Education Level Distribution (Polar Chart)",
font = list(size = 20, family = "Arial", color = "#333333")),
polar = list(
angularaxis = list(
rotation = 90,
direction = "clockwise",
tickfont = list(size = 12, color = "#333333"),
showline = TRUE,
linecolor = "#cccccc"),
radialaxis = list(showticklabels = FALSE)))
# Display the chart
fig
age_breaks <- c(18, 20, 30, 40, 50, 60, 70, 80) # Custom age breaks
pyramid_data <- viz_df %>%
mutate(age_group = cut(age_years,
breaks = c(18, 20, 30, 40, 50, 60, 70, Inf),
labels = c("18-20", "20-30", "30-40", "40-50",
"50-60", "60-70", "70+"),
right = FALSE)) %>%
mutate(phq9_binary = ifelse(phq9_total_score >= (median(phq9_total_score)), 1, 0)) %>%
count(phq9_binary, age_group) %>%
group_by(age_group) %>%
mutate(percent = (n / sum(n)) * 100,
percent = ifelse(phq9_binary == 0, -percent, percent),
phq9_binary = factor(phq9_binary, levels = c(0, 1),
labels = c("Low Depression", "High Depression")))
fig <- plot_ly(pyramid_data,
x = ~percent,
y = ~age_group,
color = ~phq9_binary,
type = "bar",
orientation = "h") %>%
layout(title = " Distribution of Depression vs. Age",
barmode = "relative",
xaxis = list(title = "Count", showgrid = FALSE),
yaxis = list(title = "Age Group"),
legend = list(x = 1, y = 1))
fig
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels